use bevy::color::palettes::basic::SILVER;
use bevy::color::palettes::css::RED;
use bevy::{prelude::*, render::view::RenderLayers};
#[cfg(feature = "panorbit")]
use bevy_panorbit_camera::{PanOrbitCamera, PanOrbitCameraPlugin};

use bevy_viewcube::{prelude::*, ROOT_QUAT};

fn main() {
    let mut app = App::new();

    app.add_plugins(DefaultPlugins)
        .add_plugins(BevyViewCubePlugin {
            use_powerful_viewcube: true,
        })
        .add_systems(Startup, setup);

    #[cfg(feature = "panorbit")]
    app.add_plugins(PanOrbitCameraPlugin);

    app.run();
}

#[derive(Component, Default)]
struct SmallView;

#[derive(Component, Default)]
struct Trident;

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    commands.spawn((
        // Transform::from_xyz(0.0, 0.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y) *
        Transform {
            rotation: *ROOT_QUAT,
            ..Default::default()
        },
        Camera { ..default() },
        Camera3d::default(),
        #[cfg(feature = "panorbit")]
        PanOrbitCamera {
            allow_upside_down: true,
            ..Default::default()
        },
        // Need set camera layer, or viewcube would be selected on this camera.
        RenderLayers::layer(0),
        ViewcubeBinding,
    ));
    // commands.insert_resource(ViewcubeFocusCenter(Vec3::new(2.0, 2.0, 0.0)));
    // commands.spawn(DirectionalLightBundle {
    //     ..Default::default()
    // });
    // commands.spawn(DirectionalLightBundle {
    //     transform: Transform::from_rotation(Quat::from_rotation_x(std::f32::consts::PI)),
    //     ..Default::default()
    // });

    // Trident
    commands.spawn((
        Mesh3d::from(meshes.add((BevyTridentAxis::default()))),
        Transform::from_translation(Vec3::new(2.0, 2.0, 0.0)),
        MeshMaterial3d(materials.add(StandardMaterial::default())),
    ));

    commands.spawn((
        // mesh: meshes.add(Cuboid::default()),
        Mesh3d::from(meshes.add((Cuboid::default()))),
        Transform::from_translation(Vec3::new(2.0, 2.0, 0.0)),
        MeshMaterial3d(materials.add(Color::from(RED))),
    ));
}
